//
//  PKRadioDetailViewController.m
//  PianKe
//
//  Created by ybq on 15/12/23.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKRadioDetailViewController.h"
#import "PKRadioDetailHeaderView.h"
#import "PKRadioDetailTableViewCell.h"
#import "PKRadioDetailModel.h"
#import "PKPlayMainViewController.h"


@interface PKRadioDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *listTableView;
@property (nonatomic, strong) NSMutableArray *listDatasource;
@property (nonatomic, strong) PKRadioDetailModel *headerModel;
@property (nonatomic, strong) PKRadioDetailHeaderView *detailHeaderView;
@property (nonatomic, assign) NSInteger requestStart;

@end

@implementation PKRadioDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    // Do any additional setup after loading the view.
    _requestStart = 0;
    [self requestAllData];
    [self creatUI];
}

- (void)creatUI {
    
    self.navView = [[PKNavigationView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 45)];
    [self.navView.leftButton setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
    [self.navView.leftButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:self.navView];
    
    _listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64)];
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    
    // MJRefresh
    _listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _requestStart = 0;
        [self requestAllData];
    }];
    _listTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _requestStart += 10;
        [self requestListData];
    }];
    
    _listTableView.rowHeight = 70;
    _detailHeaderView = [[PKRadioDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 270)];
//    [self addHeaderView];
    _listTableView.tableHeaderView = _detailHeaderView;
    [_listTableView registerClass:[PKRadioDetailTableViewCell class] forCellReuseIdentifier:@"PKRadioDetailTableViewCell"];
    [self.view addSubview:_listTableView];
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)addHeaderView {
//    __weak PKRadioDetailViewController *myself = self;
//    myself.radioInfo = ^() {
//        [_detailHeaderView.mainImageView sd_setImageWithURL:[NSURL URLWithString:_headerModel.coverimg]];
//        [_detailHeaderView.headImageView sd_setImageWithURL:[NSURL URLWithString:_headerModel.icon]];
//        _detailHeaderView.unameLabel.text = _headerModel.uname;
//        _detailHeaderView.countLabel.text = [NSString stringWithFormat:@"%@",_headerModel.musicvisitnum];
//        _detailHeaderView.descLabel.text = _headerModel.desc;
//    };
//}

// 自定义请求数据的方法
- (void)requestAllData {
    
    _listDatasource = [NSMutableArray array];
    
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"07EBFC18-9BF8-47D1-9FEA-79BBF44D4C9E";
    parDic[@"version"] = @"3.0.6";
    parDic[@"radioid"] = _radioid;
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/ting/radio_detail" parDic:parDic Method:POST finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *listArray = dataDic[@"data"][@"list"];
        if (_requestStart == 0) {
            [_listDatasource removeAllObjects];
            _headerModel = nil;
        }
        for (NSDictionary *dic in listArray) {
            PKRadioDetailModel *model = [[PKRadioDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_listDatasource addObject:model];
        }
        
        NSDictionary *headerDic = dataDic[@"data"][@"radioInfo"];
        _headerModel = [[PKRadioDetailModel alloc] init];
        [_headerModel setValuesForKeysWithDictionary:headerDic];
        //_radioInfo();
        [_listTableView.mj_header endRefreshing];
        [_listTableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

// 请求上拉数据
- (void)requestListData {
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"limit"] = @"10";
    parDic[@"start"] = [NSString stringWithFormat:@"%ld",_requestStart];
    parDic[@"radioid"] = _radioid;
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/ting/radio_detail_list" parDic:parDic Method:POST finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *listArray = dataDic[@"data"][@"list"];
        for (NSDictionary *dic in listArray) {
            PKRadioDetailModel *model = [[PKRadioDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_listDatasource addObject:model];
        }
        [_listTableView.mj_footer endRefreshing];
        [_listTableView reloadData];
    } error:^(NSError *error) {
        
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listDatasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PKRadioDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKRadioDetailTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _listDatasource[indexPath.row];
    
    if (!cell.model.isnew) {
        [cell.NewImageView removeFromSuperview];
    }
    [_detailHeaderView.mainImageView sd_setImageWithURL:[NSURL URLWithString:_headerModel.coverimg]];
    [_detailHeaderView.headImageView sd_setImageWithURL:[NSURL URLWithString:_headerModel.icon]];
    _detailHeaderView.unameLabel.text = _headerModel.uname;
    _detailHeaderView.countLabel.text = [NSString stringWithFormat:@"%@",_headerModel.musicvisitnum];
    _detailHeaderView.descLabel.text = _headerModel.desc;
    self.navView.title.text = _headerModel.title;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PKPlayMainViewController *playVC = [[PKPlayMainViewController alloc] init];
    playVC.listArray = _listDatasource;
    playVC.index = indexPath.row;;
    playVC.tingid = ((PKRadioDetailModel *)_listDatasource[indexPath.row]).tingid;
    [self.navigationController pushViewController:playVC animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
